<template>
  <div>
    <el-form inline :model="queryData" ref="queryData" class="query-form">
      <el-form-item label="省:" prop="parentId">
        <el-cascader v-model="queryData.parentId" ref="cascader" :props="props" style="width: 100%" clearable @clear="formClear('parentId')"/>
      </el-form-item>
      <el-form-item label="城市名称:" prop="name">
        <el-input v-model="queryData.name" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('name')"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查 询</el-button>
        <el-button @click="onReset">重 置</el-button>
        <el-button type="primary" @click="lookOverEvent(null)">新 增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableList" border style="width: 100%;">
      <el-table-column prop="id" label="ID" align="center" width="80"/>
      <el-table-column prop="parentId" label="父级ID" align="center"/>
      <el-table-column prop="name" label="城市名称" align="center"/>
      <el-table-column prop="createTime" label="创建时间" align="center"/>
      <el-table-column label="状态" align="center" width="150">
        <template slot-scope="{row}">
          <el-switch :value="row.type" active-text="启用" inactive-text="禁用" active-value="1" inactive-value="0"
                     @change="switchChange($event, row)"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template slot-scope="{row}">
          <el-button type="text" @click="deleteEvent(row)">删 除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Paging v-model="queryData" @change="getRequest"></Paging>
    <el-drawer title="热门城市信息" :visible.sync="drawer" direction="rtl" :before-close="drawerClose" size="500px"
               destroy-on-close class="drawer-css">
      <HotCityForm :entity="entity" v-if="drawer" @close="drawerClose" @refresh="getRequest"></HotCityForm>
    </el-drawer>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { getProvinces } from '@/utils/common.js'

  export default {
    computed: {
      ...mapGetters(['confirm'])
    },
    components: {
      HotCityForm: () => import('./HotCityForm.vue')
    },
    name: 'hotCity',
    data() {
      return {
        tableList: [],
        queryData: {
          parentId: null,
          name: null,
          pageNum: 1,
          pageSize: 10,
          total: 0
        },
        entity: null,
        drawer: false,
        props: getProvinces(0)
      }
    },
    beforeMount() {
      this.getRequest()
    },
    methods: {
      deleteEvent(row) {
        this.$confirm(this.confirm.text, '提示', this.confirm.options).then(async() => {
          await this.$ajax.get(`/wx/cities/remove?id=${row.id}`)
          this.$message.success('操作成功')
          this.onSubmit()
        })
      },
      async switchChange(type, row) {
        await this.$ajax.post('/wx/cities/openclose', { type, id: row.id })
        this.$message.success('操作成功')
        this.getRequest()
      },
      /**
       * 新增 or 查看按钮事件
       */
      lookOverEvent(row) {
        this.entity = row ? JSON.parse(JSON.stringify(row)) : null
        this.drawer = true
      },
      /**
       * 关闭抽屉
       */
      drawerClose() {
        this.drawer = false
        this.entity = null
      },
      /**
       * 重置事件
       */
      onReset() {
        this.queryData.parentId = null
        this.queryData.name = null
        this.$refs['queryData'].resetFields()
        this.getRequest()
      },
      /**
       * 查询条件提交事件
       */
      onSubmit() {
        this.queryData.pageNum = 1
        this.getRequest()
      },
      /**
       * 列表请求事件
       */
      async getRequest() {
        const params = JSON.parse(JSON.stringify(this.queryData))
        params.parentId = params.parentId && params.parentId.length > 0 ? params.parentId.shift() : null
        const res = await this.$ajax.get('/wx/cities/list', { params })
        this.queryData.total = res.total
        this.tableList = res.rows
      },
      /**
       * form-input 置空事件
       */
      formClear(key) {
        this.queryData[key] = null
        this.onSubmit()
      }
    }
  }
</script>

<style scoped>

</style>
